{
  "name": "Exit Clearance & FnF Settlement Tracking Agent",
  "nodes": [
    {
      "parameters": { "updates": ["message"] },
      "id": "tg-trigger-exit",
      "name": "Telegram Trigger",
      "type": "n8n-nodes-base.telegramTrigger",
      "typeVersion": 1.2,
      "position": [0, 0]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            { "name": "user_message", "value": "={{ $json.message.text }}", "type": "string" },
            { "name": "user_name", "value": "={{ $json.message.from.first_name + ' ' + ($json.message.from.last_name || '') }}", "type": "string" },
            { "name": "chat_id", "value": "={{ $json.message.chat.id }}", "type": "string" }
          ]
        }
      },
      "id": "set-exit-context",
      "name": "Workflow Configuration",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [220, 0]
    },
    {
      "parameters": {
        "promptType": "define",
        "text": "=User Name: {{ $json.user_name }}\nUser Message: {{ $json.user_message }}\nChat ID: {{ $json.chat_id }}",
        "options": {
          "systemMessage": "=You are an Exit Clearance and Full-and-Final Settlement Tracking Assistant.\n\nYour responsibilities:\n1. Identify whether the employee wants to REGISTER an exit clearance request or CHECK FnF settlement status.\n\n2. For new exit requests, collect:\n   - Employee ID\n   - Last Working Day\n   - Department\n   - Exit Type (Resignation / Termination / Retirement)\n\n3. Validate the employee using the Employee Exit Master Tool and extract:\n   - Employee_Name\n   - Employee_Email (for communication)\n   - Employment_Status\n\n4. If the employee is valid and exit is approved:\n   - Generate an exit reference ID in format:\n     EXIT-YYYYMMDD-[last 4 digits of employee]-HHmmss\n   - Register the exit using the Exit Clearance Register Tool\n   - Send acknowledgement email using Gmail Tool to Employee_Email\n   - Explain clearance steps and FnF settlement timeline\n   - Respond using Telegram Response Tool\n\n5. If the employee is invalid or exit not approved:\n   - Politely explain the reason\n   - Do NOT register the exit\n   - Respond using Telegram Response Tool\n\n6. For status checks:\n   - Ask for exit reference ID if missing\n   - Retrieve status using the FnF Settlement Status Tool\n   - Explain current clearance / settlement stage\n   - Respond using Telegram Response Tool\n\nStrict rules:\n- Do NOT promise settlement dates.\n- Do NOT provide legal advice.\n- Email must be retrieved from master sheet only.\n- ALWAYS respond using the Telegram Response Tool."
        }
      },
      "id": "agent-exit",
      "name": "Exit Clearance & FnF Agent",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "typeVersion": 3,
      "position": [500, 0]
    },
    {
      "parameters": {
        "model": { "mode": "id", "value": "gpt-4o" },
        "options": { "temperature": 0.2 }
      },
      "id": "llm-exit",
      "name": "OpenAI Chat Model",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "typeVersion": 1.3,
      "position": [320, 220]
    },
    {
      "parameters": {
        "sessionIdType": "customKey",
        "sessionKey": "={{ $('Workflow Configuration').item.json.chat_id }}"
      },
      "id": "memory-exit",
      "name": "Simple Memory",
      "type": "@n8n/n8n-nodes-langchain.memoryBufferWindow",
      "typeVersion": 1.3,
      "position": [500, 220]
    },
    {
      "parameters": { "descriptionType": "manual" },
      "id": "exit-master",
      "name": "Employee Exit Master Tool",
      "type": "n8n-nodes-base.googleSheetsTool",
      "typeVersion": 4.7,
      "position": [640, 260]
    },
    {
      "parameters": { "operation": "append", "descriptionType": "manual" },
      "id": "exit-register",
      "name": "Exit Clearance Register Tool",
      "type": "n8n-nodes-base.googleSheetsTool",
      "typeVersion": 4.7,
      "position": [800, 260]
    },
    {
      "parameters": { "descriptionType": "manual" },
      "id": "fnf-status",
      "name": "FnF Settlement Status Tool",
      "type": "n8n-nodes-base.googleSheetsTool",
      "typeVersion": 4.7,
      "position": [960, 320]
    },
    {
      "parameters": {
        "sendTo": "={{ $fromAI('employee_email') }}",
        "subject": "Exit Clearance & FnF Process Initiated",
        "message": "Your exit clearance request has been registered. You will be notified as clearances and FnF settlement progress."
      },
      "id": "gmail-exit",
      "name": "Exit Clearance Notification Email",
      "type": "n8n-nodes-base.gmailTool",
      "typeVersion": 2.1,
      "position": [1120, 220]
    },
    {
      "parameters": {
        "chatId": "={{ $('Telegram Trigger').item.json.message.chat.id }}",
        "text": "={{ $json.output }}"
      },
      "id": "tg-response-exit",
      "name": "Send Telegram Response",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [960, 0]
    }
  ],
  "connections": {
    "Telegram Trigger": { "main": [[{ "node": "Workflow Configuration", "type": "main" }]] },
    "Workflow Configuration": { "main": [[{ "node": "Exit Clearance & FnF Agent", "type": "main" }]] },
    "Exit Clearance & FnF Agent": { "main": [[{ "node": "Send Telegram Response", "type": "main" }]] }
  },
  "active": false
}
